package com.goldcn.classify.service;

import com.goldcn.classify.dao.ClassifyAttrDefMapper;
import com.goldcn.classify.dto.AttrbuteExtendDto;
import com.goldcn.classify.dto.ClazzObjDto;
import com.goldcn.classify.model.ClassifyAttrDef;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import com.goldcn.config.PropertiesConfig;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.util.CommonTools;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/25.
 */
@Service("classifyAttrDefService")
@Transactional
public class ClassifyAttrDefService {

    @Autowired
    private ClassifyAttrDefMapper classifyAttrDefMapper;

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private PlatformService platformService;
    @Autowired
    PropertiesConfig cfg;  // 配置信息

    private Logger logger = Logger.getLogger(ClassifyAttrDefService.class);

    /**
     * 通过平台类型获取属性列表
     *
     * @param code
     * @return
     */
    public List<ClassifyAttrDef> selectAttrDefList(String code) {
        try {
            return classifyAttrDefMapper.selectAttrDefList1(Integer.parseInt(code));
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.parmError);
        }
    }

    /**
     * 查询指定平台中所有属性的详细信息
     *
     * @param code
     */
    public Object selectAttributiInfoList(String code) {

        return classifyAttrDefMapper.selectAttributeInfoList(code);


    }

    /*************************************/


    public int deleteByPrimaryKey(String cfadID) {
        return classifyAttrDefMapper.deleteByPrimaryKey(cfadID);
    }

    public String insertSelective(ClassifyAttrDef record) {
        record.setCfadID(UuidUtils.uuid());
        int i = 0;
        i = classifyAttrDefMapper.insertSelective(record);
        if (i == 1) {
            return record.getCfadID();
        } else {
            throw new ServiceException("插入属性失败", RestConst.ErrorCode.DATABASE_ERROR);
        }
    }

    public ClassifyAttrDef selectByPrimaryKey(String cfadID) {
        ClassifyAttrDef classifyAttrDef = new ClassifyAttrDef();
        classifyAttrDef = classifyAttrDefMapper.selectByPrimaryKey(cfadID);
        if (classifyAttrDef == null) {
            throw new ServiceException("属性ID不存在", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyAttrDef;
    }

    public Map<String, Object> selectByPrimaryKey_new(String cfadID) {
        Map<String, Object> stringObjectMap = classifyAttrDefMapper.selectByPrimaryKey_new(cfadID);
        if (stringObjectMap.isEmpty()) {
            throw new ServiceException("属性ID不存在", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //加入platform
        stringObjectMap.put("clazzPlatform",platformService.queryPlatformByRef(
                stringObjectMap.get("cfadID").toString()));

        stringObjectMap.put("gd_val_list", classifyAttrDefMapper.selectAttrValList(cfadID));
        return stringObjectMap;
    }

    public int updateByPrimaryKeySelective(ClassifyAttrDef record) {
        int i = classifyAttrDefMapper.updateByPrimaryKeySelective(record);
        if (i != 1) {
            throw new ServiceException("修改属性失败", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return i;
    }


    public AttrbuteExtendDto loadAttrInfo_new(ClazzObjDto list) {
        AttrbuteExtendDto attrbuteExtendDto = new AttrbuteExtendDto();

        List<Map<String, Object>> goodsAttrList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> standardAttrList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> attrList = new ArrayList<Map<String, Object>>();
        List<Map<String, String>> brandinfoList = new ArrayList<Map<String, String>>();

        Map<String, Object> map = new HashMap<String, Object>();

        try {
            attrList.addAll(classifyAttrDefMapper.selectAttrList(list.getClazzObjID(), list.getPlatform()));
            for (int j = 0; j < attrList.size(); j++) {

                List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
                mapList = classifyAttrDefMapper.selectAttrValList(attrList.get(j).get("gd_cfadID").toString());
                attrList.get(j).put("gd_attrval_list", mapList);
                if (Integer.parseInt(attrList.get(j).get("gd_clazzType").toString()) == 1) {
                    goodsAttrList.add(attrList.get(j));
                } else if (Integer.parseInt(attrList.get(j).get("gd_clazzType").toString()) == 2) {
                    if("weight".equals(attrList.get(j).get("clazzAttrCode"))){
                        attrbuteExtendDto.setHaveWeight(true);
                    }
                    standardAttrList.add(attrList.get(j));
                }

            }
            attrbuteExtendDto.setGoodsAttrList(goodsAttrList);
            attrbuteExtendDto.setStandardAttrList(standardAttrList);
            return attrbuteExtendDto;

        } catch (ServiceException e) {
            throw new ServiceException("品牌更新失败",   "005");
        }
    }

    public Object loadAttrInfo(ClazzObjDto list, String shopID) {
        AttrbuteExtendDto aed = this.loadAttrInfo_new(list);
//        if (CommonUtils.isNotEmpty(shopID)) {
//            DataResult result = restTemplate.getForObject(cfg.getUserUrl() + "/shopInfo/brand/" + shopID, DataResult.class);
//            if (result.getIsSuccess() == 0 || result.getData() == null) {
//                throw new ServiceException(result.getErrorDesc(),result.getErrorCode());
//            }else{
//                aed.setBrandList(result.getData());
//            }
//        }
        return aed;
    }

}
